package com.shadow.cloud.test.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.*;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Arrays;

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate() {
        // 绕过SSL证书校验
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory() {
            @Override
            protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
                if (connection instanceof HttpsURLConnection) {
                    prepareHttpsConnection((HttpsURLConnection) connection);
                }
                super.prepareConnection(connection, httpMethod);
            }

            private void prepareHttpsConnection(HttpsURLConnection connection) {
                connection.setHostnameVerifier(new SkipHostnameVerifier());
                try {
                    connection.setSSLSocketFactory(createSslSocketFactory());
                } catch (Exception ex) {
                    // Ignore
                }
            }

            private SSLSocketFactory createSslSocketFactory() throws Exception {
                SSLContext context = SSLContext.getInstance("TLS");
                context.init(null, new TrustManager[] { new SkipX509TrustManager() }, new SecureRandom());
                return context.getSocketFactory();
            }

            class SkipHostnameVerifier implements HostnameVerifier {
                @Override
                public boolean verify(String s, SSLSession sslSession) {
                    return true;
                }
            }

            class SkipX509TrustManager implements X509TrustManager {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }

                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType) {

                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType) {

                }
            }
        };
        requestFactory.setConnectTimeout(300000);
        requestFactory.setReadTimeout(300000);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_HTML, MediaType.TEXT_PLAIN));

        restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
        return restTemplate;
    }
}
